import sqlite3
import requests
import time

print("Ctrl+c 退出")
conn = sqlite3.connect('./sajjtk.db')
cursor = conn.cursor()
url = 'https://sajj.jlntech.cn/sajj/gthmmc/index.php?m=ias&c=manager&a=getQuestion&from=xcx&destination_id=499&type=1,2,3,4,5,6,7,8,9,10,11,'
headers = {
    'User-Agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
for i in range(200):
    print('*' * 10, i, '*' * 10)
    resp = requests.get(url=url, headers=headers)
    datalist = (resp.json()["datalist"])
    for data in datalist:
        sqltxt = f'''INSERT INTO sajj(title,a,b,c,d,answer) VALUES("{data['title']}","{data['chose_a']}","{data['chose_b']}","{data['chose_c']}","{data['chose_d']}","{data['answer']}")'''
        # print(sqltxt)
        try:
            cursor.execute(sqltxt)
            with open('./title.txt', 'a', encoding='utf-8') as f:
                f.write(data['title'] + '\n')
            conn.commit()
            print(sqltxt)
        except Exception as e:
            # print(e)
            pass
    time.sleep(1)

conn.close()
# for data in datalist:
